package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.ChapterAndVideoVo.ChapterVo;
import com.atguigu.eduservice.entity.ChapterAndVideoVo.VideoVo;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-02-10
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;


    @Override
    public List<ChapterVo> getAllChapterAndVideo(String courseId) {

        //1.根据courseId查询所有的课程章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
        //2.根据courseId查询所有的课程小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(wrapperVideo);

        //创建一个ChapterVo集合来接收最终的数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3.遍历一个课程里面所有的课程章节
        for (int i = 0; i < eduChapters.size(); i++) {

            EduChapter eduChapter = eduChapters.get(i);
            //创建自己的课程章节vo
            ChapterVo chapterVo = new ChapterVo();
            //赋值
            BeanUtils.copyProperties(eduChapter,chapterVo);


            //4.遍历所有的课程小节  并把每个课程小节放入对应的章节
            //创建一个课程小节集合用于临时放数据
            List<VideoVo> VideoVoList = new ArrayList<>();
            for (int m = 0; m < eduVideos.size(); m++) {

                EduVideo eduVideo = eduVideos.get(m);
                //如果遍历出来的课程小节的章节Id和课程章节的Id一样的才放进集合
                if(eduChapter.getId().equals(eduVideo.getChapterId())){
                    //创建自己的课程小节vo
                    VideoVo videoVo = new VideoVo();
                    //赋值
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //添加到集合里
                    VideoVoList.add(videoVo);
                }


            }
            chapterVo.setChildren(VideoVoList);


            //添加到最终返回的集合
            finalList.add(chapterVo);

        }


        return  finalList;
    }

    /**
     * 如果章节中存在小节则不能删除章节
    **/
    @Override
    public boolean deleteChapterById(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        //查询此章节是否有小节
        wrapper.eq("chapter_id",chapterId);
        //章节中的小节数
        int count = eduVideoService.count(wrapper);
        if(count >0 ){
            throw new MyException(20001,"章节存在小节，不能删除章节");
        }else{
            int flag = baseMapper.deleteById(chapterId);
            //flag>0 则返回true
            return flag>0;
        }


    }

    /**
     * 根据课程id删除课程章节
     * //1.删除课程里面的章节
     *
    **/
    @Override
    public void deleteCourseInfo(String courseId) {

        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
